package com.cdie.myschool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdie.myschool.domain.entity.Calendar;
import com.cdie.myschool.domain.entity.EnjoyRunningInfo;
import com.cdie.myschool.domain.entity.EnjoyRunningInfoOld;
import com.cdie.myschool.mapper.CalendarMapper;
import com.cdie.myschool.mapper.EnjoyRunningMapper.EnjoyRunningInfoMapper;
import com.cdie.myschool.mapper.EnjoyRunningMapper.EnjoyRunningInfoOldMapper;
import com.cdie.myschool.service.EnjoyRunningInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class EnjoyRunningInfoServiceImpl implements EnjoyRunningInfoService {
    @Autowired
    private CalendarMapper calendarMapper;

    @Autowired
    private EnjoyRunningInfoMapper enjoyRunningInfoMapper;
    @Autowired
    private EnjoyRunningInfoOldMapper enjoyRunningInfoOldMapper;

    @Override
    public void importData() {
        int size = 1000;
        importEnjoyRunningInfoRuId();
    }

    /**
     * 导入学期id：semester_id
     * @return
     */
    @Override
    public boolean importEnjoyRunningInfoRuId() {
        int currentPage = 1;
        int pageSize = 10;
        Page<EnjoyRunningInfoOld> page = null;
        Page<EnjoyRunningInfoOld> enjoyRunningInfoOldPage = null;
        while (true) {
            page = new Page<>(currentPage++, pageSize);
            enjoyRunningInfoOldPage = enjoyRunningInfoOldMapper.selectPage(page, null);
            //从原始数据源获取原始数据
            List<EnjoyRunningInfoOld> enjoyRunningInfoOldList = enjoyRunningInfoOldPage.getRecords();
            if (!CollectionUtils.isEmpty(enjoyRunningInfoOldList)) {
                //防止死循环
                enjoyRunningInfoOldList.forEach((item) -> {
                    //转换成我的数据
                    EnjoyRunningInfo enjoyRunningInfo = new EnjoyRunningInfo(item);
                    //查询跑步信息表对应的学期,并从第一个‘-’分隔开

                    //1.查出原表的学期字段
                    String[] c1 = item.getXnxq().split("-");
                    String one = c1[0];
                    String two = c1[1];
                    String three = c1[2];
                    String three1 = null;
                    if (Integer.parseInt(three)==1){
                        three1 =String.valueOf( Integer.parseInt(three) - 1+12);
                    }else if (Integer.parseInt(three) != 1 && Integer.parseInt(three)>0){
                        three1 ="0"+String.valueOf( Integer.parseInt(three) - 1);
                    }else {

                        System.out.println("出错了！");
                    }
                    System.out.println(one);
                    System.out.println(two);
                    System.out.println(three1);
                    //2.查找学期id
                    //SELECT semester_id FROM calendar WHERE begin_time LIKE '2021%' and end_time LIKE '2022-01%' ;
                    LambdaQueryWrapper<Calendar> calendarLambdaQueryWrapper = new LambdaQueryWrapper<Calendar>();
                    Calendar calendar = calendarMapper.selectOne(calendarLambdaQueryWrapper.likeRight(Calendar::getBeginTime,one).likeRight(Calendar::getEndTime,two+"-"+three1));
                    if (calendar == null){
                        System.out.println("出错了！");

                    }
                    enjoyRunningInfo.setSemesterId(calendar.getSemesterId());
                    //插入到目标数据源
                    enjoyRunningInfoMapper.insert(enjoyRunningInfo);
                });
                log.info("ScientificPapersInfo:执行数据导入:第{}页数据导入成功", enjoyRunningInfoOldPage.getCurrent());
            } else {
                log.info("ScientificPapersInfo:执行数据导入:第{}页数据为空,停止导入", enjoyRunningInfoOldPage.getCurrent());
                break;
            }
        }
        log.info("ScientificPapersInfo:执行数据导入完成,总计导入{}条数据", enjoyRunningInfoOldPage.getTotal());
        return false;
    }

}
